<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;
use Illuminate\Support\Facades\Cache;

class LoginController extends Controller
{
    //登录视图
    public function index(){
        if (auth()->check()){
            return redirect(route('admin.index'));
        }
        return view('admin.login.login');
    }
    //登录处理
    public function login(Request $request){
        //验证码处理
       /* $captch=$request->post('captch');
            if (!BaseController::valicode($captch)){
                //销毁session
                \Session::forget('captch');
                    return redirect(route('admin.login'))->withErrors(['error'=>'验证码错误']);
             }*/
        $post=$this->validate($request,[
            'username'=>'required',
            'password'=>'required',
        ]);


       $bool=auth()->guard('web')->attempt($post);

    if ($bool){
        //日志
        //写入权限
        $adminModel=auth()->user();
        $roleModel=$adminModel->role;
        $nodeid=$roleModel->nodes()->pluck('route_name','id')->toArray();
        //把节点权限保存到session中
        session(['admin.auth'=>$nodeid]);
        return redirect(route('admin.index'));
    }
    return redirect(route('admin.login'))->withErrors(['errar'=>'账号或者密码错误']);
    }
}
